package com.libinyn.base.pms.controller.sys;


import com.libinyn.base.pms.common.annotation.SysLog;
import com.libinyn.base.pms.common.exception.PmsException;
import com.libinyn.base.pms.common.utils.Constant;
import com.libinyn.base.pms.common.utils.Result;
import com.libinyn.base.pms.controller.AbstractController;
import com.libinyn.base.pms.entity.sys.SysMenuEntity;
import com.libinyn.base.pms.serivce.sys.ShiroService;
import com.libinyn.base.pms.serivce.sys.SysMenuService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * 系统菜单管理
 * @fileName: SysMenuController.java
 * @author: bin.li01
 * @date: 2017/12/10 15:45 
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

	@Resource
	private SysMenuService sysMenuService;
	@Resource
	private ShiroService shiroService;

	/**
	 * 导航菜单
	 * @return
	 */
	@RequestMapping("/nav")
	public Result nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		Set<String> permissions = shiroService.getUserPermissions(getUserId());
		return Result.ok().put("menuList", menuList).put("permissions", permissions);
	}
	
	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public List<SysMenuEntity> list(){
		List<SysMenuEntity> menuList = null;
		try {
			menuList = sysMenuService.selectAll();
		} catch (Exception e) {
			logger.error("查询菜单失败",e);
		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public Result select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return Result.ok().put("menuList", menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public Result info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu;
		try {
			menu = sysMenuService.selectByPrimaryKey(menuId);
		} catch (Exception e) {
			logger.error("查询菜单信息失败！{}{}",menuId,e);
			return Result.error("查询菜单失败");
		}
		return Result.ok().put("menu", menu);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@RequestMapping("/save")
	@RequiresPermissions("sys:menu:save")
	public Result save(@RequestBody SysMenuEntity menu){


		try {
			//数据校验
			verifyForm(menu);
			sysMenuService.insertSelective(menu);
		} catch (Exception e) {
			logger.error("保存菜单失败",e);
			return Result.error("保存菜单失败");
		}

		return Result.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@RequestMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public Result update(@RequestBody SysMenuEntity menu){
		try {
			//数据校验
			verifyForm(menu);

			sysMenuService.updateByPrimaryKey(menu);
		} catch (Exception e) {
			logger.error("更新菜单失败",e);
			return Result.error("更新菜单失败");
		}

		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:menu:delete")
	public Result delete(long menuId){
		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return Result.error("请先删除子菜单或按钮");
		}

		sysMenuService.deleteBatch(new Long[]{menuId});
		
		return Result.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu) throws Exception {
		if(StringUtils.isBlank(menu.getName())){
			throw new PmsException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new PmsException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new PmsException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.selectByPrimaryKey(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new PmsException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new PmsException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
